var bulk = db.MostActiveReport.initializeUnorderedBulkOp();
//For All Companies remove the condition in the below find method.
var myDocuments = db.MostActiveReport.find({});
var ops = 0

print(new Date())

//Get All companies with the names into a dictionary
var companies = {};
db.getSiblingDB("ClientDW_B").CorporateData.find({}, { MainExchange: 1, MainTicker: 1 }).forEach(function(item){
    var key1 = item._id + "_exchange";
    var key2 = item._id + "_ticker"
    companies[key1] = item.MainExchange;
    companies[key2] = item.MainTicker;
});

//For each portfolio details update the company name
myDocuments.forEach(
    function(myDoc){
        var key1 = myDoc._id + "_exchange";
        var key2 = myDoc._id + "_ticker"
        
        bulk.find({_id: myDoc._id}).updateOne(
            {
                $unset: {
                    Exchanges: 0,
                    Tickers: 0
                },
                $set: { 
                    MainExchange: companies[key1],
                    MainTicker: companies[key2],
                }
            }
        );
  
        if ((++ops %1000) === 0){
            bulk.execute();
            bulk = db.MostActiveReport.initializeUnorderedBulkOp();
        }
    }
)
    
bulk.execute();
print(new Date())